<template>
  <div class="roleManagement-container">
    <el-divider content-position="left"></el-divider>
    <vab-query-form>
      <vab-query-form-left-panel :span="12"></vab-query-form-left-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      :element-loading-text="elementLoadingText"
      @selection-change="setSelectRows"
    >
      <el-table-column show-overflow-tooltip type="selection"></el-table-column>

      <el-table-column
        show-overflow-tooltip
        width="250"
        prop="paper.title"
        label="名称"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="paper.state"
        label="状态"
      ></el-table-column>
      <el-table-column show-overflow-tooltip prop="count" label="已收/待批阅">
        <template #default="{ row }">
          {{ row.count }} / {{ row.toReadCount }}
        </template>
      </el-table-column>

      <el-table-column show-overflow-tooltip label="操作" width="80">
        <template #default="{ row }">
          <el-button type="text" @click="handleRevew(row)">批阅</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :page-size="queryForm.pageSize"
      :layout="layout"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></el-pagination>
    <edit ref="edit" :id-key="idkey" @fetch-data="fetchData"></edit>
  </div>
</template>

<script>
  import { getAllPaper } from '/src/api/grade'
  import Edit from './components/ManagementEdit'
  import { defaultPageSize } from '/src/config'

  export default {
    name: 'Management',
    components: { Edit },
    data() {
      return {
        idkey: 'id',
        list: null,
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        elementLoadingText: '正在加载...',
        queryForm: {
          pageNo: 1,
          pageSize: defaultPageSize,
          order: {
            //  sort: 'asc'
          },
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      setSelectRows(val) {
        this.selectRows = val
      },
      handleRevew(row) {
        let path = `/grade/answerSheetList/${row.paper.id}`
        console.log(path)
        this.$router.push({ path: path })
      },
      handleEdit(row) {
        if (row) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNo = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNo = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true

        const resp = await getAllPaper(this.queryForm)
        const data = resp.data

        this.list = data

        setTimeout(() => {
          this.listLoading = false
        }, 300)
      },
    },
  }
</script>
